

require('./common');

exports.load = function(req, res){
	var id = req.params.id;
	var tblname = req.params.tblname;
	var formName = req.params.formName;
	var newDate = new Date();
	var rCount=0;
	var users=["Stephen Cury","Harrison Barnes", "Kent Bazemore", "Andris Biedrins", "Andrew Bogut", "Festus Ezeli", "Draymond Green"];
	
	var strSQL = "SELECT * FROM " + tblname + ' ORDER BY ID DESC LIMIT 10 ';
	
	conn.query(strSQL, function(err, results, fields){
		if(err) throw err;
		
		var output = '<html><head></head><body  style="background-color:#fafafa"><h4>Home / Forms /</h4><h2><img src="/images/Posts.png" style="vertical-align:middle" style="border:0px">&nbsp;' + formName +'</h2><table cellpadding=0 cellspacing=0 width="98%">';
		output += '<tr style="background-color:#f4f4f4">';
		output += '<td colspan=2><a href="form/' + id+ '/0/" class="bluebutton">New Request</a></td>';
		output += '<td align="right"><input type="text" value="Search..." style="width:200px;font-style:Italic;color:#777" /></td>';
		output += '</tr>';
		output += '<tr>';
		output += '<td colspan=3><hr></td>';
		output += '</tr>';
		output += '<tr>';
		output += '<td colspan=3>';
		for (var index in results) {
			
			output += '<div class="requestview">';
			output += '<div style="float:left;padding-right:10px">';
			output += '<img src="/images/' +  results[index].ID +'.jpg" style="vertical-align:middle">';
			output += '</div>';
			output += '<div class="requestBox">';
			output += '<h4 style="display: inline;">'+ users[index] +'</h4><br/><br/>';
			var sqlResult = results[index];
			var fieldName = fields[1].name;
			var fieldValue = sqlResult[fieldName];
			output += '<font color="#777" style="font-weight:normal"> submitted a request with Tracking Number: </font>'	
			output += '<a href="form/' + id+ '/' + results[index].ID + '/" class="editDelete">' + results[index].TrackNo + '</a>';
			output += '<ul style=" padding: 0;list-style-type: none;">';
			output += '<li style="float: left"><a href="form/' + id+ '/' + results[index].ID + '/" class="editDelete">View</a></li>';
			output += '<li style="float: left"><a href="form/' + id+ '/' + results[index].ID + '/" class="editDelete">Approve</a></li>';
			output += '<li style="float: left"><a href="form/' + id+ '/' + results[index].ID + '/" class="editDelete">Reject</a></li>';
			output += '<li style="float: left"><a href="form/' + id+ '/' + results[index].ID + '/" class="editDelete">Comment</a></li>';
			output += '<li style="float: left"><font color="#777"><abbr class="timeago" title="' + results[index].DateCreated + '"> '+ results[index].DateCreated +'</abbr></li>';
			output += '</ul>';
			output += '</div><br style="clear:both;"/>';
			output += '</div><br/>';
			rCount++;
		}
		
		if(rCount==0){
			output += '<tr style="background-color:#f2f0bf">';
			output +=  '<td colspan=3 style="border:1px Solid #808a10;padding:10px" valign="top">No record found.</td>';
			output +=  '</tr>';
		}
		output += '</td>';
		output += '</tr>';
		output += '</table>';
		output += '</body></html>';
		
		
		res.writeHead(200, {'Content-Type': 'text/html'});
		res.end(output);
	});
}